package database;

import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.Statement ;
/**
 * An example taken from work where these connection succeed. 
 * @author User
 *
 */
public class LocalDatabaseConnection {
//        /**
//         * Example of the use
//         * @return
//         */
//        public static Connection fetchReportCardConnection() {
//                Connection oracleConnection = createConnection(DatabaseType.ORACLE, // Connection Type
//                                "jdbc:oracle:thin:@dwops.wnyric.org:1521:escprod",  // URL 
//                                "REPORTCARDS",                                      // username
//                                "WNYRICWHS"                                         // password
//                                ) ;
//                return oracleConnection ;
//        }
//       
//        public static Connection fetchReportCardConnection(int year) {
//                Connection oracleConnection = createConnection(DatabaseType.ORACLE,
//                                "jdbc:oracle:thin:@dwops.wnyric.org:1521:escprod",
//                                "REPORTCARDS_" + year,
//                                "WNYRICWHS"
//                                ) ;
//                return oracleConnection ;
//        }
//       
//       
//       
//        public static void main(String[] arguments) {
////                String url = "jdbc:oracle:thin:@dwops.wnyric.org:1521:escprod" ;
////                String username = "REPORTCARDS" ;
////                String password = "WNYRICWHS" ;
////                Connection mainReportCardConnection = createConnection(DatabaseType.ORACLE, url, username, password) ;
//                Connection mainReportCardConnection = fetchReportCardConnection() ;
//                boolean success = pingConnection(mainReportCardConnection, DatabaseType.ORACLE) ;
//                if(success) {
//                        System.out.println("ping successful for " + "main report card connection") ;
//                }
//                else {
//                        System.out.println("ping failed for " + "main report card connection") ;
//                }
//
//                Connection latestReportCardConnection = fetchReportCardConnection(2011) ;
//                boolean success2011 = pingConnection(latestReportCardConnection, DatabaseType.ORACLE) ;
//                if(success2011) {
//                        System.out.println("ping successful for " + "2011 connection") ;
//                }
//                else {
//                        System.out.println("ping failed for " + "2011 connection") ;
//                }
//        }
//
//        public static Connection createConnection(String databaseType, String url, String userName, String password) {
//                Connection connection = null ;
//                String driverClassName = "" ;
//                if(DatabaseType.DB2.equals(databaseType)) {
//                        driverClassName = "com.ibm.db2.jcc.DB2Driver" ;
//                }
//                else if(DatabaseType.POSTGRES.equals(databaseType)) {
//                        driverClassName = "org.postgresql.Driver" ;
//                }
//                else if(DatabaseType.ORACLE.equals(databaseType)) {
//                        driverClassName = "oracle.jdbc.driver.OracleDriver" ;
//                }
//                else {
//                        return null ; // invalid database type
//                }
//               
//               
//                try {
//                        Class.forName(driverClassName);
//                        connection = DriverManager.getConnection(
//                                        url,
//                                        userName,
//                                        password ) ;
//                }
//                catch (SQLException sqlException) {
//                        sqlException.printStackTrace();
//                }
//                catch (ClassNotFoundException classNotFoundException) {
//                        System.out.println("database driver not found") ;
//                        classNotFoundException.printStackTrace() ;
//                }
//                return connection ;
//        }
//
//        /**
//         * Returns true when the connection has been verified as open
//         * @param connection
//         * @param databaseType
//         * @return
//         */
//        public static boolean pingConnection(Connection connection, String databaseType) {
//                String pingSql = "" ;
//                if(DatabaseType.DB2.equals(databaseType)) {
//                        pingSql = "select 1 from SYSIBM.SYSDUMMY1 " ;
//                }
//                else if(DatabaseType.POSTGRES.equals(databaseType)) {
//                        pingSql = "select version()" ;
//                }
//                else if(DatabaseType.ORACLE.equals(databaseType)) {
//                        pingSql = "select 1 from DUAL " ;
//                }
//                else {
//                        return false ; // invalid database type
//                }
//               
//                boolean isConnected = true ;
//                try {
//                        Statement pingStatement =  connection.createStatement( ) ;
//                        isConnected = pingStatement.execute(pingSql) ;
//                }
//                catch (SQLException exception) {
//                        exception.printStackTrace();
//                        isConnected = false ;
//                }
//                return isConnected ;
//        }
//

}

//public class DatabaseType {
//        public static String DB2 = "DB2" ;
//        public static String POSTGRES = "Postgres" ;
//        public static String ORACLE = "Oracle" ;
//        public static String MYSQL = "MySql" ;
//        
//}
